/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
function checkComboBoxValue(id) {
    var value = $(id).combobox('getValue');
    var valueField = $(id).combobox("options").valueField;
    var allData = $(id).combobox("getData");
    var result = false;
    for (var i = 0; i < allData.length; i++) {
        if (value == allData[i][valueField]) {
            result = true;
            break;
        }
    }
    return result;
}

$.extend($.fn.validatebox.defaults.rules, {
    selectValueRequired: {
        validator: function(value, param) {
            //console.info($(param[0]).find("option:contains('" + value + "')").val());
            return checkComboBoxValue(param[0]);
        },
        message: '该输入项为必选项'
    },
    /*10:05:02*/
    time: {
        validator: function(value) {
            var a = value.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
            if (a == null) {
                return false;
            } else if (a[1] > 24 || a[3] > 60 || a[4] > 60) {
                return false;
            }
        },
        message: '时间格式不正确，请重新输入。'
    },
    /*2017-02-01*/
    date: {
        validator: function(value) {
            var r = value.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
            if (r == null) {
                return false;
            }
            var d = new Date(r[1], r[3] - 1, r[4]);
            return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
        },
        message: '时间格式不正确，请重新输入（格式：2020-01-01）。'
    },
    /*2014-01-01 13:04:06*/
    datetime: {
        validator: function(value) {
            var r = value.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
            if (r == null)
                return false;
            var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
            return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7]);
        },
        message: '时间格式不正确，请重新输入(格式：2020-01-01 13:04:06)。'
    },
    phoneNum: {
        validator: function(value) {
            return /^1[3|4|5|7|8|9]\d{9}$/.test(value);
        },
        message: '手机号码格式不正确，请重新输入。'
    },
    idNum: {
        validator: function(value) {
            return /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(value);
        },
        message: '身份证号码格式不正确，请重新输入。'
    }

});

